package Bio::Rambam::Proteomics::Searches::XTANDEM_search;
use Moose;
use namespace::autoclean;
with 'Bio::Rambam::Proteomics::Tools::ENGINE_search';
use Bio::Rambam::Proteomics qw(%ENZYMES %MODS $TEMPLATES);
use Data::Dumper;
use File::Basename;
use IPC::System::Simple qw(capture);
use Try::Tiny;

has 'search'	=>	(is=>'rw',	isa=>'Bio::Rambam::Proteomics::Search',	default=>sub{{}}, trigger =>\&_start_xtandem);

sub _start_xtandem{
	my $self=shift;
	my $search=$self->search;
	###	Paths
	my $xtandem_path=$search->engines->xtandem;
	die "XTandem path is not correct.\n You should use the complete path to the executable file.\n" unless -B$xtandem_path;
	my $xtandem_out_folder=$search->folder."/xtandem";	#omly works for linux
	my $xtandem_out_file = basename($search->input_file);
	$xtandem_out_file =$xtandem_out_folder.'/'.$xtandem_out_file;
	$xtandem_out_file=~s/\.mgf/\.xml/;
	mkdir $xtandem_out_folder unless -d$xtandem_out_folder;
	my $xtandem_default_input_file=$xtandem_out_folder.'/'."xtandem_default_input.xml";
	my $xtandem_taxonomy_file=$xtandem_out_folder.'/'."xtandem_taxonomy.xml";
	#######	Taxonomy file
	my %replacements_taxonomy;
	$replacements_taxonomy{database}=$search->database;
	my $xtandem_taxonomy_template=$TEMPLATES."xtandem_taxonomy.xml";
	my $xtandem_taxonomy_text=template($xtandem_taxonomy_template,\%replacements_taxonomy);
	open my $output_taxonomy,">",$xtandem_taxonomy_file;
	print $output_taxonomy $xtandem_taxonomy_text;
	close $output_taxonomy;
	#######	Default_input file
	my %replacements;
	$replacements{taxonomy_file}=$xtandem_taxonomy_file;
	$replacements{input_file}=$search->input_file;
	$replacements{output_file}=$xtandem_out_file;
	$replacements{miss_cleavages}=$search->miss_cleavages;
	if($search->threads==0){
		$replacements{threads}=4;
	}
	else{
		$replacements{threads}=$search->threads;
	}
	$replacements{enzyme}=$ENZYMES{$search->enzyme}{Regex};
	$replacements{peptide_tol}=($search->peptide_tol)/2;
	$replacements{fragment_tol}=$search->fragment_tol;
	if($search->pep_tol_units eq "Da"){
		$replacements{pep_tol_units}="Daltons";
	}
	else{
		$replacements{pep_tol_units}="ppm";
	}
	$replacements{top_peptide_charge}=$search->top_peptide_charge;
	
	
	################		 Modifications
	my $fixed_mods;
	my $variable_mods;
	foreach(split /\,/,$search->fixed_mods){
		my ($mod,$res)=split /\@/;
		$fixed_mods.=$MODS{$mod}{'mass'}."@";
		$fixed_mods.=$res if length($res)==1;
		$fixed_mods.="["  if $res eq 'Nt';
		$fixed_mods.="]"  if $res eq 'Ct';
		$fixed_mods.=",";
	}
	foreach(split /\,/,$search->variable_mods){
		my ($mod,$res)=split /\@/;
		$variable_mods.=$MODS{$mod}{'mass'}."@";
		$variable_mods.=$res if length($res)==1;
		$variable_mods.="["  if $res eq 'Nt';
		$variable_mods.="]"  if $res eq 'Ct';
		$variable_mods.=",";
	}
	$replacements{text_fixed_mods}=$fixed_mods;
	$replacements{text_var_mods}=$variable_mods;
	################


	my ($fragx,$fragy,$fragz,$fraga,$fragb,$fragc)=("no","no","no","no","no","no");
	if($search->fragmentation eq 'CID'){
		$fragb=$fragy="yes";
	}
	elsif($search->fragmentation eq 'ECD' or $search->fragmentation eq 'ETD'){
		$fragx=$fragy=$fragz="yes";
	}
	$replacements{fragx}=$fragx;
	$replacements{fragy}=$fragy;
	$replacements{fragz}=$fragz;
	$replacements{fraga}=$fraga;
	$replacements{fragb}=$fragb;
	$replacements{fragc}=$fragc;
	
	my $xtandem_default_input_template=$TEMPLATES."xtandem_default_input.xml";
	my $xtandem_taxonomy_default_input_text=template($xtandem_default_input_template,\%replacements);
	open my $output,">",$xtandem_default_input_file;
	print $output $xtandem_taxonomy_default_input_text;
	close $output;
	
	my $command="$xtandem_path $xtandem_default_input_file";
	run_command($command,$self);
	$self->output_file ($xtandem_out_file);
}

__PACKAGE__->meta->make_immutable;